package org.dromara.answer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.answer.domain.AiOptions;
import org.dromara.answer.domain.bo.AiOptionsBo;
import org.dromara.answer.service.IAiOptionsService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.answer.domain.bo.AiQuestionsBo;
import org.dromara.answer.domain.vo.AiQuestionsVo;
import org.dromara.answer.domain.AiQuestions;
import org.dromara.answer.mapper.AiQuestionsMapper;
import org.dromara.answer.service.IAiQuestionsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 题目Service业务层处理
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@RequiredArgsConstructor
@Service
public class AiQuestionsServiceImpl extends ServiceImpl<AiQuestionsMapper, AiQuestions> implements IAiQuestionsService {

    private final AiQuestionsMapper baseMapper;
    private final IAiOptionsService optionsService;

    /**
     * 查询题目
     *
     * @param questionId 主键
     * @return 题目
     */
    @Override
    public AiQuestionsVo queryById(Long questionId){
        AiQuestionsVo aiQuestionsVo = baseMapper.selectVoById(questionId);
        aiQuestionsVo.setOptions(optionsService.queryListByQuestionId(questionId));
        return aiQuestionsVo;
    }

    /**
     * 分页查询题目列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 题目分页列表
     */
    @Override
    public TableDataInfo<AiQuestionsVo> queryPageList(AiQuestionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AiQuestions> lqw = buildQueryWrapper(bo);
        Page<AiQuestionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的题目列表
     *
     * @param bo 查询条件
     * @return 题目列表
     */
    @Override
    public List<AiQuestionsVo> queryList(AiQuestionsBo bo) {
        LambdaQueryWrapper<AiQuestions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiQuestions> buildQueryWrapper(AiQuestionsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AiQuestions> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), AiQuestions::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionType()), AiQuestions::getQuestionType, bo.getQuestionType());
        lqw.eq(bo.getDifficulty() != null, AiQuestions::getDifficulty, bo.getDifficulty());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), AiQuestions::getCategory, bo.getCategory());
        lqw.eq(bo.getStatus() != null, AiQuestions::getStatus, bo.getStatus());
        lqw.eq(bo.getScore() != null, AiQuestions::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getExplanation()), AiQuestions::getExplanation, bo.getExplanation());
        lqw.orderByDesc(AiQuestions::getCreateTime);
        return lqw;
    }

    /**
     * 新增题目
     *
     * @param bo 题目
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AiQuestionsBo bo) {
        AiQuestions add = MapstructUtils.convert(bo, AiQuestions.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        optionsService.saveOrUpdateByBo(bo.getOptions(), add.getQuestionId());
        if (flag) {
            bo.setQuestionId(add.getQuestionId());
        }
        return flag;
    }

    /**
     * 修改题目
     *
     * @param bo 题目
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AiQuestionsBo bo) {
        AiQuestions update = MapstructUtils.convert(bo, AiQuestions.class);
        validEntityBeforeSave(update);
        boolean b = baseMapper.updateById(update) > 0;
        optionsService.saveOrUpdateByBo(bo.getOptions(), update.getQuestionId());
        return b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AiQuestions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除题目信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

}
